﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using VM.BLL.ShopCar;
using VM.Common.EntityEnum;
using VM.Common.Result;
using VM.Common.strFormatUntil;
using VM.Entity.EntityDto.Order;
using VM.Entity.EntityDto.ShopCar;
using VM.IBLL.IOrder;
using VM.IBLL.IShopCar;
using VM.IBLL.System.Code;
using VM.WebCore.Jwt;
using VM.WebCore.SwaggerExtend;
using VM.Wechat;

namespace VM.WebAPI.Controllers
{

    /// <summary>
    ///订单相关接口
    /// </summary>
    [Route("api/order")]
    [ApiController]
    public class OrderController : ControllerBase
    {
        private readonly ICodeHelperBLL _ICodeHelperBLL;
        private readonly IOrderBLL _IOrderBLL;
        private readonly IJWTService _IJWTService;
        private readonly IHttpContextAccessor _HttpContextAccessor;

        public OrderController(IJWTService iJWTService, ICodeHelperBLL iCodeHelperBLL, IHttpContextAccessor httpContextAccessor, IOrderBLL iOrderBLL)
        {

            _ICodeHelperBLL = iCodeHelperBLL;
            _IJWTService = iJWTService;
            _HttpContextAccessor = httpContextAccessor;
            _IOrderBLL = iOrderBLL;
        }
        /// <summary>
        /// 添加订单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过] PC
        [Route("addOrder")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiDataResult<List<getOrderMainInfoDto>> addOrder(addMultyOrderDto obj)
        { 
            //解析Jwt,获取到UserId
            int UserId= _IJWTService.GetUserIdByAuthInfoStr();
            //解析并优化类的传参
            obj= _ICodeHelperBLL.perfectClass<addMultyOrderDto>(obj,UserId,ProTypeEnum.save);

            return _IOrderBLL.addOrder(obj, UserId);
        }

        /// <summary>
        /// 获取我的订单列表，   PHONE
        /// </summary>
        /// <param name="OrderInfo"></param>
        /// <param name="status">订单状态 0 全部  1待付款 2待使用  3已使用  4已取消  5已过期  999已删除</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("getMyOrderList")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiDataResult<PagingData<getOrderListDto>> getMyOrderList(int status, string OrderInfo="", int pageIndex=1, int pageSize=100)
        { 
           //解析Jwt,获取到UserId
            int UserId = _IJWTService.GetUserIdByAuthInfoStr();
            ApiDataResult<PagingData<getOrderListDto>> Res = _IOrderBLL.getOrderList(UserId,status,OrderInfo,pageIndex, pageSize);
            return Res;
        }

        /// <summary>
        /// 获取订单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("getOrderInfoById")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiDataResult<getOrderInfoDto>  getOrderInfoById(int id)
        {

            ApiDataResult<getOrderInfoDto> Res = _IOrderBLL.getOrderInfoById(id);
            return Res;
        }


        /// <summary>
        /// 根据订单内码删除订单记录
        /// </summary>
        /// <param name="OrderIds">订单列表的拼接id</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("deleteOrderByIds")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult deleteShopCarById(string OrderIds)
        {
            StrFormat sf = new StrFormat();
            int Count = sf.GetCount(",", OrderIds) + 1;
            //检查订单状态
            int id = 0;
            ApiDataResult<getOrderInfoDto> Res = null;
            for (int i = 0; i < Count; i++)
            {
                id = Convert.ToInt32( sf.GetSplitStr(OrderIds,',',i));
                Res = _IOrderBLL.getOrderInfoById(id);
                //等待付款或者已取消
                if ( Res.data.status != 4 && Res.data.status != 3) {
                    ApiResult erroRes = new ApiResult()
                    {
                        code=403,
                        msg="非已取消、已使用状态"

                    };
                    return erroRes; 
                } 
            } 
            //解析Jwt,获取到UserId
            int UserId = _IJWTService.GetUserIdByAuthInfoStr();
             
            int OrderId = 0;
            int DelCount = 0;
            for (int i = 0; i < Count; i++)
            {
                OrderId = Convert.ToInt32(sf.GetSplitStr(OrderIds, ',', i));
                ApiResult DeleteRes = _IOrderBLL.deleteOrderById(UserId, OrderId);
                if (DeleteRes.code == 200)
                {
                    DelCount++;
                }
            }
            ApiResult endRes = new ApiResult()
            {
                code = 201,
                msg = $@"成功删除{DelCount}条数据"


            };
            return endRes;
        }
         
        /// <summary>
        /// 根据订单内码取消订单
        /// </summary>
        /// <param name="id">订单列表的拼接id</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("cancleOrderById")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult cancleOrderById(int id)
        {
             
            ApiDataResult<getOrderInfoDto> Res = null;
         
            Res = _IOrderBLL.getOrderInfoById(id);
             
                if (Res.data.status != 1 )
            {
                Res = _IOrderBLL.getOrderInfoById(id);
                ApiResult erroRes = new ApiResult()
                {
                    code = 403,
                    msg = "只有待付款订单可取消"

                };
                return erroRes;
            }
               
           ApiResult CancelRes = _IOrderBLL.cancelOrderById(id);  
            return CancelRes;
        }


        /// <summary>
        /// 批量更新订单状态
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("updateOrderStatusByIds")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult updateOrderStatusByIds(List<updateOrderStatusByIdDto> obj)
        {
            //解析Jwt,获取到UserId
            int UserId = _IJWTService.GetUserIdByAuthInfoStr();
            //解析并优化类的传参
            return _IOrderBLL.updateOrderStatusByIds(obj, UserId);
        }
         
        /// <summary>
        /// 更新订单数量
        /// </summary>
        /// <param name="goodId"></param>
        /// <param name="specId"></param>
        /// <param name="qty"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize] 
        [Route("updateStockQty")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult updateStockQty(List<updateStockQtyDto> obj)
        { 

            ApiResult Res = _IOrderBLL.updateOrderQty(obj);
            return Res;
        }

        /// <summary>
        /// 检查库存是够足够
        /// </summary>
        /// <param name="specId"></param>
        /// <param name="qty">要下单的数量</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("checkStockQty")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult checkStockQty(List<updateStockQtyDto> obj)
        {
            ApiResult Res = _IOrderBLL.checkStockQty(obj);
            return Res;
        }

    }
}
